package com.jjh.web.controller;

import com.jjh.Application;
import com.jjh.pojo.Order;
import com.jjh.pojo.User;
import com.jjh.service.impl.OrderServiceImpl;
import com.jjh.util.FileUtils;
import com.jjh.winterframework.annatation.Autowired;
import com.jjh.winterframework.annatation.Component;
import com.jjh.service.UserSerice;
import com.jjh.service.impl.UserServiceImpl;
import com.jjh.web.view.UserView;
import com.jjh.wrapper.QueryWrapper;

import java.util.List;
import java.util.Objects;
import java.util.Scanner;

/**
 * 用户显示类
 */
@Component
public class UserController implements UserView {

    private static Scanner scanner = new Scanner(System.in);


    @Autowired
    private  UserServiceImpl userSerice;

    @Autowired
    private OrderServiceImpl orderService;

    /**
     * 用户登录功能
     * @return
     */
    @Override
    public boolean login(){
        return userSerice.login();
    }

    /**
     * 用户注册功能
     * @return
     */
    @Override
    public boolean register(){

        return userSerice.register();
    }

    /**
     * 用户中心
     */
    @Override
    public void UserInfo() {

        System.out.println("<========================" +
                "============= 用 户 中 心 ======================================>");

        System.out.println(" -> 1.查看个人信息");
        System.out.println(" -> 2.修改信息");
        System.out.println(" -> 3.查看订单信息");
        System.out.println(" -> 0.返回上一级");
        System.out.println();
        boolean flag = true;

        while (flag){
            System.out.print(" 请输入你的选择:");
            String str = scanner.nextLine();
            switch (str){
                case "1":
                     showUserInfo(Application.threadLocal.get());
                    break;
                case "2":
                     updateInfo(Application.threadLocal.get());
                    break;
                case "3":
                    showOrderInfo(Application.threadLocal.get());
                    break;

                case "0":
                    flag = false;
                    break;
                default:
                    System.out.println("=====>输入不合法! 请重新输入:");

            }



        }










    }

    /**
     * 展示用户个人信息
     * @param user
     */
    private void showUserInfo(User user) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",user.getUsername());
        User user1 = userSerice.getUser(wrapper);
        System.out.println("<===================================== 个人信息 如下 ==========" +
                "========================>");
        System.out.println("  <用户名>    :| "+user1.getUsername()+" |");
        System.out.println("  <用户密码>   :| "+user1.getPassword()+" |");
        System.out.println("  <年龄>      :| "+user1.getAge()+" |");
        System.out.println("  <收货地址>   :| "+user1.getAddress()+" |");
        System.out.println("  <电话>      :| "+user1.getPhone()+" |");

    }

    /**
     * 修改用户信息
     * @param user
     */
    private void updateInfo(User user) {
        System.out.println("==========>你想要修改哪个信息:");
        System.out.println(" -> 1.修改密码");
        System.out.println(" -> 2.修改年龄");
        System.out.println(" -> 3.修改收货地址");
        System.out.println(" -> 4.修改电话");
        System.out.println(" -> 0.返回上一级");
        boolean flag = true;

        while (flag){
            System.out.println("请输入选择");
            String str = scanner.nextLine();
            switch (str){
                case "1":
                    while (true){
                        System.out.print(" -> 请输入原密码:");
                        String pass1 = scanner.nextLine();
                        if(!pass1.equals(user.getPassword())){
                            System.out.println("原密码不正确! 请重新输入");
                            continue;
                        }
                        System.out.print(" -> 请输入新密码:");
                        String pass2 = scanner.nextLine();

                            while (true){
                                System.out.println(" -> 你确定修改吗? (yes/no)");
                                String chi = scanner.nextLine();
                                if(chi.equals("yes")){
                                    user.setPassword(pass2);
                                    userSerice.updateOne(user);
                                    System.out.println(" => 修改成功！");
                                    break;
                                } else if (chi.equals("no")) {
                                    break;
                                }else {
                                    System.out.println("===> 输入错误! 请重新输入:");
                                }
                            }
                            break;
                    }
                    break;
                case "2":
                    while (true){
                        System.out.print(" -> 请输入修改的年龄值:");
                        int age = scanner.nextInt();
                        if(age<=0||age>100){
                            System.out.println("年龄不合法! 请重新输入:");
                        }else {
                            user.setAge(age);
                            userSerice.updateOne(user);
                            System.out.println(" => 修改成功!");
                            break;
                        }
                    }
                    break;

                case "3":
                     while (true){
                         System.out.print(" -> 请输入修改的地址值:");
                         String address = scanner.nextLine();
                         if(address==""){
                             System.out.println("输入的地址为空! 请重新输入:");
                         }else{
                             user.setAddress(address);
                             userSerice.updateOne(user);
                             System.out.println(" => 修改成功！");
                             break;
                         }
                     }
                    break;
                case "4":
                    while (true){
                        System.out.print(" -> 请输入要修改的电话号码:");
                        String phone = scanner.nextLine();
                        if(phone==""){
                            System.out.println("输入电话不合法，请重新输入:");
                        }else {
                            user.setPhone(phone);
                            userSerice.updateOne(user);
                            System.out.println("修改成功！");
                            break;
                        }
                    }
                    break;

                case "0":
                    flag = false;
                    break;
                default:
                    System.out.println("======>输入不合法，请重新输入:");
            }

        }


    }

    /**
     * 展示订单
     * @param user
     */
    private void showOrderInfo(User user) {

        User user1 = userSerice.getUser(new QueryWrapper().eq("username", user.getUsername()));
        int id = user1.getId();

        System.out.println("ID  商家Id  菜品Id  菜品名称  数量  单价  总价  收货地址   备注  下单时间");
        List<Order> orders = orderService.getOrderByUserId(id);
        for (Order order : orders) {
            System.out.println(order.getOId()+" "+order.getShopId()+" "+
                    order.getDishId()+" "+order.getDName()+" "+
                    order.getCount()+" "+order.getSinglePrice()+" "+
                    order.getAllPrice()+" "+order.getAddress()+" "+
                    order.getDesc()+" "+order.getTime());
        }
    }


}
